package src
{
		
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	import flash.text.TextFieldAutoSize;
	
	import gs.TweenMax;
	
	import nl.demonsters.debugger.MonsterDebugger;
	

	public class Pantalla_Galeria extends Pantalla
	{
		private var connection:NetConnection;
		private var gateway:String = 'http://www.ladespensa.es/gonzalo/springfield/amfphp/gateway.php';
		private var listaFotos:Object;
		private var _cont:Sprite;
		private var numPags:Number;
		private var pagActual = 0;
		private var paginacion:Sprite;
		private var i:int;
		private var foto:FotoConcurso;
		private var pag:Pag;
		private var deb = new MonsterDebugger(this);
		
		public function Pantalla_Galeria()
		{
			this.x = 65.50;
			this.y = 30.30;
			
			montarGaleria();
		}
		
		private function montarGaleria():void
		{
			connection = new NetConnection();
			connection.connect(gateway);
			connection.call("ContactService.dameGaleria",new Responder(recogerFotos));
		}
		
		private function recogerFotos (fotos:Object):void
		{
			listaFotos = fotos;
			numPags = Math.ceil(listaFotos.length / 6);
			_cont = new Sprite();
			_cont.x = this.cabecera.x;
			_cont.y = (this.cabecera.y + this.cabecera.height) + 10;
			addChild(_cont);
			
			if(numPags > 1){
				creaPaginacion();	
			}
			
			pintaFotos();
		}
		
		private function creaPaginacion ():void
		{
			var np:String;
			paginacion = new Sprite();
			for(i=0; i < numPags; i++){
				pag = new Pag();
				if(numPags < 10){
					np = '0'+(i+1);
				}else{
					np = String(i+1);
				}
				pag.texto_txt.text = np;
				pag.texto_txt.autoSize = TextFieldAutoSize.LEFT;
				pag.x = i * (pag.width + 5);
				pag.mouseChildren = false;
				pag.buttonMode = true;
				pag.addEventListener(MouseEvent.CLICK, clicPag);
				paginacion.addChild(pag);
			}
			paginacion.name = 'paginacion';
			navegacion.addChild(paginacion);
			navegacion.init();
		}
		
		private function clicPag (e:MouseEvent):void
		{
			var _clip = e.currentTarget;
			var _n:Number = Number(_clip.texto_txt.text);
			pagActual = _n - 1;
			var _obj;
			for(i=0; i < _cont.numChildren; i++){
				_obj = _cont.getChildAt(i);
				TweenMax.to(_obj, 0.3, {alpha:0, onCompleteParams:[_obj, i], onComplete:function(_obj, i){
					_cont.removeChild(_obj);
					if((i+1) == _cont.numChildren){pintaFotos();}
				}});
				
			}
			pintaFotos();
		}
		
		private function pintaFotos ():void
		{
			var fotosPantalla:int;
			var pant:int = 6 * pagActual;
			
			
			if(pant+6 > listaFotos.length){
				fotosPantalla = listaFotos.length - pant;
			}else{
				fotosPantalla = 6;
			}
			
			
			var _clip;
			var posX;
			var posY = -199.90;
			for(i=0; i < fotosPantalla; i++){
				_clip = listaFotos[i+(pagActual*6)];
				foto = new FotoConcurso();
				foto.carga(_clip);
				if(i%3 == 0){
					posY += foto.height + 5;
					posX = 0;
				}else{
					posX += foto.width + 12;
				}
				foto.x = posX;
				foto.y = posY;
				_cont.addChild(foto);
			}
			
		}
		
		
	}
}